﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Common;
using He.Web.UI;
using System.Collections.Generic;

namespace He.Web.manage.olservice
{
    public partial class edit : Manage
    {
        private string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!Page.IsPostBack)
            {
                ddlIdType.DataSource = EnumHelper.GetList(typeof(Enums.OlserviceType));
                ddlIdType.DataTextField = "Text";
                ddlIdType.DataValueField = "Text";
                ddlIdType.DataBind();

                ddlGroupBind();

                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("editolservice");
                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("addolservice");
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.olservice model = new BLL.olservice().GetModel(id);
            txtTitle.Text = model.title;
            txtUserId.Text = model.user_id;
            txtPort.Text = model.port;
            txtSortId.Text = model.sort_id.ToString();
            chkRed.Checked = model.is_red == 1 ? true : false;
            ddlIdType.SelectedValue = model.id_type.ToString();
            ddlGroupId.SelectedValue = model.group_id.ToString();
        }
        #endregion

        #region 绑定分组=================================
        private void ddlGroupBind()
        {
            List<Model.olservice_group> lst = new BLL.olservice_group().GetModelList("id>0");
            ddlGroupId.Items.Add(new ListItem("请选择分组", ""));
            foreach (Model.olservice_group item in lst)
            {
                ddlGroupId.Items.Add(new ListItem(item.group_name, item.id.ToString()));
            }
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.olservice bll = new BLL.olservice();
            Model.olservice model = new Model.olservice();
            model.title = txtTitle.Text.Trim();
            model.user_id = txtUserId.Text.Trim();
            model.port = txtPort.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.is_red = chkRed.Checked == true ? 1 : 0;
            model.id_type = ddlIdType.SelectedValue;
            model.group_id = int.Parse(ddlGroupId.SelectedValue);
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[在线客服管理]增加内容:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.olservice bll = new BLL.olservice();
            Model.olservice model = bll.GetModel(_id);
            model.is_red = chkRed.Checked == true ? 1 : 0;
            model.user_id = txtUserId.Text.Trim();
            model.port = txtPort.Text.Trim();
            model.sort_id = int.Parse(txtSortId.Text.Trim());
            model.title = txtTitle.Text.Trim();
            model.id_type = ddlIdType.SelectedValue;
            model.group_id = int.Parse(ddlGroupId.SelectedValue);
            if (bll.Update(model))
            {
                SaveLogs(string.Format("[在线客服管理]修改内容:{0}", model.title));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("修改信息成功啦！","list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    ShowError("保存过程中发生错误啦！");
                    return;
                }
                ShowSuccess("添加信息成功啦！", "list.aspx");
            }
        }
    }
}
